/**
 * 
 */
package com.desksoft.ecommerce.util.remoteio;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.desksoft.ecommerce.util.remoteio.realize.LocalIOUtil;
import com.desksoft.ecommerce.util.remoteio.realize.NfsRemoteIOUtil;
import com.desksoft.ecommerce.util.remoteio.realize.SFTPRemoteIOUtil;
import com.google.common.base.Enums;
import com.google.common.collect.ImmutableMap;

/***************************************
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: Apr 28, 2012
 * 创建作者：yejw
 * 文件名称：RemoteIOFactory.java
 * 版本：1.0
 * 功能：
 * 最后修改时间：Apr 28, 2012
 * 修改记录：
 * </pre>
 *****************************************/
public class RemoteIOFactory {
	
	/**
	 * 
	 */
	protected final Log log = LogFactory.getLog(RemoteIOFactory.class);
	
	/**
	 * Remote IO Settings
	 */
	private RemoteIOSetting settings;
	
	/**
	 * 
	 */
	protected static Map<PROTOCOL, Class<? extends RemoteIOUtil>> protocols = ImmutableMap.of(
			PROTOCOL.NFS, NfsRemoteIOUtil.class,
			PROTOCOL.SFTP, SFTPRemoteIOUtil.class,
			PROTOCOL.LOCAL, LocalIOUtil.class
	);
	
	protected RemoteIOFactory() { }
	
	/**
	 * 传输协议
	 */
	enum PROTOCOL {
		NFS,
		
		SFTP,
		
		LOCAL,
		
		FTP
	}
	
	/**
	 * Returns the default RemoteIOUtil instance
	 * 
	 * @param settings
	 * @return
	 */
	public static RemoteIOUtil getInstance(RemoteIOSetting settings) {
		RemoteIOFactory ioFactory = new RemoteIOFactory();
		ioFactory.setSettings(settings);
		
		return ioFactory.getInstance();
	}
	
	/**
	 * Returns the default RemoteIOUtil instance
	 * 
	 * @return
	 */
	public RemoteIOUtil getInstance() {
		return getInstance(Enums.valueOfFunction(PROTOCOL.class).apply(settings.getProtocol()));
	}
	
	/**
	 * Returns the RemoteIOUtil instance
	 * 
	 * @return
	 */
	public RemoteIOUtil getInstance(PROTOCOL type) {
		try {
			RemoteIOUtil io = protocols.get(type).newInstance();
			io.setIOSettings(settings);
			io.initRemote();
			
			return io;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * @param settings the settings to set
	 */
	public void setSettings(RemoteIOSetting settings) {
		this.settings = settings;
	}
	
}
